package com.moran.controller;

import ai.z.openapi.ZhipuAiClient;
import ai.z.openapi.service.embedding.EmbeddingCreateParams;
import ai.z.openapi.service.embedding.EmbeddingResponse;
import ai.z.openapi.service.embedding.EmbeddingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;

import java.util.Arrays;

/**
 * 嵌入模型
 */
@RestController
public class EmbeddingController {

    @Value("${spring.ai.zhipuai.base-url}")
    private String baseUrl;

    @Value("${spring.ai.zhipuai.api-key}")
    private String apiKey;

    @Value("${spring.ai.zhipuai.embedding.options.model}")
    private String embeddingModel;

    /**
     * 使用指定模型获取文本嵌入向量
     *
     * @return
     */
    @RequestMapping("/embed")
    public EmbeddingResult embed(String text) {
        // 初始化客户端
        ZhipuAiClient client = ZhipuAiClient.builder()
                .baseUrl(baseUrl)
                .apiKey(apiKey)
                .build();

        // 创建向量化请求（自定义维度）
        EmbeddingCreateParams request = EmbeddingCreateParams.builder()
                .model(embeddingModel)
                .input(Arrays.asList(text))
                .dimensions(768)  // 指定768维
                .build();

        // 发送请求
        EmbeddingResponse response = client.embeddings().createEmbeddings(request);
        return response.getData();
    }

}
